package com.xbd.wxpay.controller;





import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xbd.wxpay.util.RSAUtil;
import lombok.extern.slf4j.Slf4j;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.ui.Model;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;


import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.*;



@Slf4j
@RestController
@RequestMapping("/index")
public class IndexController {
    private static final String CHANNEL_PRIVATE_KEY = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBALZxHOf/miBlSyMr8a8Ve/XZ4Di/4rrIplUVgwa5OCBtwUXMjtn5O/NrKCZPYNXAi7Hrs4J+TUcavobmrXxNtWhkCIoLE1ws983vgTiqhJXldTLDvjt/TGR+VQAUOZyS6aVBLpnQIGnziKwEiSCcpJ+yw0tDSbbu+QxYXmh7U4pnAgMBAAECgYEAgLCBB1TQzxEkUwboT+wDqIOrGdygfciTK2klJ1T3Nki1EBsemLqpgyaPB5buY1iSMUplMSMON52xz05gCVJ3Jp2WHSQxECez9YCnP9PQ5Sd9hgFsyjC9zffDc7R61+pVMlwBec4wbAVcb2F52Wle3lJq+Dpj+SXOXpghOHIwpQECQQDq75sGD8B4fbG6Hk5idT0gIQjRPryZkDb/bdhUPOUZGvLB4e3jy0LMq+R8rmD/wrTN8SzkUKIJHGCgAS441ZlhAkEAxsyhWFs8aCTMuiI1jldgbtYMq1r+K0Rvpv0QzUOCZTNkaHZwQAHO77QmdalhQOjf7ANB2XeiH5F3NVHFtDtQxwJALLAa+tIwODsgfViabFwEnoHqHXMjiMpHy9pnFc7nfRkFKCADkhrtv+TKd3Tr+npczfecJGm6CKWfM5BF7IWkwQJBAKqwXAAPJgiNQDK42J8Eqsm/g1VFZWIk+nC4bEqxoi3eOGHw/2QVqWXqmfMAoX4n9Msh6NIdg9fzABWJxmR/YVUCQHTZsAbmNB11TohHq0RUTNtGI+84E1XTpCWTc4Zozb46yeAz++wsRN7M5FKd3MQWZUOJnGeAliH+HjJ5CTIgsmU=";

    private static final String HZYF_PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCtfQpEuzXj6N4an2ceJjLbEXN5bbMu3kDlJukJXMc3z6MHBp45ZO3/NXL8eNLRNc7t0PclllZripfRYrlNeSwCEJlQrTi0E7nlE6hl64rUhaRhERIYpFEfmbrfGauw+BrOYeMGjoR8fJphvc5xjExDnWyv6X1nw3fGZjjP8l+usQIDAQAB";

    @RequestMapping(method = RequestMethod.POST,value = "/api")
    public String api(@RequestBody  MyData myData) throws Exception {

        Map<String, Object> params = new HashMap();
        params.put("channelId", "GONDOLA_01");
        params.put("timeStamp", String.valueOf(new Date().getTime()));
        final JSONObject requestData = new JSONObject();
        requestData.put("phoneList", myData.phones);
        params.put("requestData", requestData);
        System.out.println(params);
        try {
            // aes加密业务参数
            String aesKey = RSAUtil.generateAesKey();
            String encryptRequestData = RSAUtil.aesEncrypt(requestData.toString(), aesKey);
            params.put("requestData", encryptRequestData);

            // rsa加密key
            String encryptAesKey = RSAUtil.encrypt(aesKey, RSAUtil.getPublicKey(HZYF_PUBLIC_KEY));
            params.put("key", encryptAesKey);

            // sha1WithRsa签名
            String signatureContent = RSAUtil.getSignatureContent(params);
            String sign = RSAUtil.sha1Sign(signatureContent, RSAUtil.getPrivateKey(CHANNEL_PRIVATE_KEY));
            params.put("sign", sign);

            System.out.println(params);
        } catch (Exception e) {
            System.out.println(e);
        }
        return JSONObject.toJSONString(params);



    }


}
